package com.blog.service.Impl;
import com.blog.bean.domain.Answer;
import com.blog.bean.domain.Examine;
import com.blog.bean.domain.UserExamine;
import com.blog.bean.dto.ExamineDTO;
import com.blog.bean.dto.UserExamineDTO;
import com.blog.bean.vo.ExamineVo;
import com.blog.mapper.AnswerMapper;
import com.blog.mapper.ExamineMapper;
import com.blog.mapper.UserExamineMapper;
import com.blog.service.ExamineService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author 任浩
 * @version 1.0
 */
@Slf4j
@Service
public class ExamineServiceImpl implements ExamineService {

    @Autowired
    private ExamineMapper examineMapper;

    @Autowired
    private AnswerMapper answerMapper;
    @Autowired
    private UserExamineMapper userExamineMapper;

    private static final int NUM = 9;

    @Override
    public ExamineVo getExameneList() {
        //根据题目所存在题目的数量为基数，随机生成五个随机数，生成的随机数的作为获取题目的id
        //使用Math.random来生成随机数,生成的数值在0~1.0之间
        int [] random = new int[5];
        for (int i = 0; i < 5; i++){
            int a = (int) (Math.random()* NUM + 1);
            random[i] = a;
            /**
             * 这里这样写是为了生成随机不重复的数据
             */
            for (int j = 0 ; j < i; j++){
                if (random[j] == a){
                    i--;
                    break;
                }
            }
        }

        Arrays.sort(random);

        log.info("随机出来的数字为: {}",random);

        List<Examine> examineList = examineMapper.selectExamineList(random);

        ExamineVo examineVo = ExamineVo.builder().examineList(examineList).build();

        log.info("从数据库查询处理的数据为： {}",examineVo);

        return examineVo;
    }

    @Override
    @Transactional
    public ExamineVo submitExamine(UserExamineDTO answer) {
        log.info("传递过来的数据为: {}",answer);
        List<ExamineDTO> array = answer.getData();

        int right = 0; //正确题目数

        //得到题目的id，用id去查找答案。
        List<Long> idList = new ArrayList<>();
        array.forEach(item -> {
            ExamineDTO data = (ExamineDTO) item;
            idList.add(data.getExamineId());
        });

        List<Answer> answerList = answerMapper.selectListAnswer(idList);
        log.info("查询出来的答案信息为: {}",answerList);

        List<UserExamine> userExamineList = new ArrayList<>();
        for (int i = 0 ; i < 5 ; i++){
            //比较答案,是否正确。
            int isRight = 0;
            if (array.get(i).getAnswer().equals(answerList.get(i).getAnswer())){
                isRight = 1;
                right++;
            }

            UserExamine userExamine = UserExamine.builder().examineId(array.get(i).getExamineId()).isRight(isRight)
                    .myAnswer(array.get(i).getAnswer()).rightAnswer(answerList.get(i).getAnswer())
                    .useTime(30)
//                    .userId(Integer.parseInt(ThreadLocalUserId.get()))
                    .userId(1)
                    .build();
            userExamineList.add(userExamine);
        }

        log.info("要插入数据库的我的做题的数据为: {}",userExamineList);
        //将自己做题的数据插入数据库。
        userExamineMapper.insertBatchs(userExamineList);

        //批量插入数据之后，将做题解析返回给前端，因为题目已经返回给前端，所以我们这里不用把题目再重新返回给前端。
        List<ExamineVo.UserExamineVo> data = new ArrayList<>();
        for (int i = 0 ; i< 5;i++){
            Answer answer1 = answerList.get(i);
            UserExamine userExamine = userExamineList.get(i);
            ExamineVo.UserExamineVo userExamineVo = new ExamineVo.UserExamineVo();

            userExamineVo.setExamineId(userExamine.getExamineId());
            userExamineVo.setMyAnswer(userExamine.getMyAnswer());
            userExamineVo.setRightAnswer(userExamine.getRightAnswer());
            userExamineVo.setIsRight(userExamine.getIsRight() == 1 ? true : false);
            userExamineVo.setUseTime(userExamineVo.getUseTime());
            userExamineVo.setAnalysis(answer1.getAnalysis());
            data.add(i,userExamineVo);
        }

        ExamineVo examineVo = ExamineVo.builder().right(right).useTime(30).userExamineList(data).build();

        return examineVo;
    }
}
